<!-- eslint-disable vue/max-attributes-per-line -->
<template>
  <div>
    御剑乘风来,除魔天地间!
    <!-- 权限点数据展示 -->
    <!-- 权限点数据展示:check-strictly="true"全选框复选框不互相打扰 -->
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button size="small" @click="showDialogAssign = false">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { assignPerm, getRoleDetail } from '@/api/settings'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData2 } from '@/utils/index'
export default {
  name: '',
  props: {
    roleId: String,
    showDialogAssign: Boolean
  },
  data() {
    return {
      permissionData: [], // 存储权限数据
      permIds: []
    }
  },
  computed: {},
  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },
  methods: {
    async loadPermissionList() {
      // 发送请求, 获取权限列表
      const { data } = await getPermissionList()
      console.log('权限列表的数据是', data)
      this.permissionData = tranListToTreeData2(data)
    },
    async loadPermissionByRoleId() {
      // 根据roleId获取当前这个角色已经具备的权限
      const res = await getRoleDetail(this.roleId)
      console.log('获取当前角色的已有的权限点数据', res.data.permIds)
      this.permIds = res.data.permIds
      // 回填
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    async hSubmit() {
      console.log(this.$refs.tree.getCheckedKeys())
      this.permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      // 关闭弹框
      this.$emit('update:showDialogAssign', false)
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
